package com.apes.scm.account.creditLine.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.creditLine.model.CreditLine;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.Map;

/**
 * @author wuq
 * @Time 2022-8-2 17:48
 * @Description 批量创建信用额度调整单
 */
@Service
public class BatchCreditLineService extends DomainService {

    @Autowired
    private PartyRoleRepository partyRoleRepository;
    @Autowired
    private CreditLineService creditLineService;
    @Autowired
    private EntityManager entityManager;

    /**
     * 入口方法
     * 虚拟客户编码 02533593
     * @param request {"companyId":"","partnerId":"","incurred":"","accountPeriod":""}
     */
    public void process(SimpleRequest request) {

        String sql = " SELECT '02533593' PARTNER_ID, ID COMPANY_ID, 30000 incurred, 180 account_Period " +
                "  FROM MAS_PARTY_ROLE A " +
                " WHERE A.ROLE_ID = 'Company' AND A.ID LIKE '6%' " +
                " AND EXISTS (SELECT 1 FROM MAS_STORE D WHERE D.COMPANY_ID = A.ID AND D.CLOSING_DAY IS NULL) " ;
        entityManager.createNativeQuery(sql)
                .unwrap(NativeQueryImpl.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
                .getResultStream().forEach(m -> {
                    JSONObject json = new JSONObject((Map) m);
                    createCreditLine(json);
                });
    }


    private void createCreditLine(JSONObject json){
        CreditLine creditLine = new CreditLine();
        creditLine.setState("create");
        creditLine.setNote("【9周年活动，系统开发阶段，抖音、京东过渡挂账使用】");

        creditLine.setIncurred(json.getDouble("INCURRED"));         // 额度
        creditLine.setAccountPeriod(json.getInteger("ACCOUNT_PERIOD"));   // 账期

        PartyRole company = partyRoleRepository.findOne(json.getString("COMPANY_ID"));
        creditLine.setCompany(company.getParty().getPartyRoleByType("Company"));
        PartyRole partner = partyRoleRepository.findOne(json.getString("PARTNER_ID"));
        creditLine.setPartner(partner.getParty().getPartyRoleByType("Customer"));

        // 获取历史信用额度
        Map<String, Double> credit = creditLineService.findLedgerByType(company, partner, "credit");
        creditLine.setHistoryLine(credit.get("credit"));

        // 获取历史账期
        Map<String, Double> period = creditLineService.findLedgerByType(company, partner, "period");
        creditLine.setHistoryAccountPeriod(period.get("period").intValue());

        // 保存 并且 审核
        creditLine = invoke("acc.creditLine.create", MapUtil.mapped(creditLine));
        invoke("acc.creditLine.approve", MapUtil.mapped(creditLine));
    }
}
